//
//  Status.swift
//  SinaWebo
//
//  Created by mis on 16/3/20.
//  Copyright © 2016年 mis. All rights reserved.
//

import UIKit

class Status: NSObject {
    /** 微博创建时间 */
    var created_at: String?
    /** 微博ID */
    var id: Int = 0
    /** 微博信息内容 */
    var text: String?
    /** 微博来源 */
    var source: String?
    /** 用户 */
    var user: User?
    // 在服务器返回的数据中 关于图片是返回的是一个只有id的数组
    var pic_urls: [[String : String]]?
    
    //计算型属性
    var imageURLs: [NSURL]? {
        guard let urls = pic_urls else {
            return nil
        }
        
        var tempArray = [NSURL]()
        for item in urls {
            //相信后端给的urlString 一定能够转换成 url
            let url = NSURL(string: item["thumbnail_pic"]!)
            tempArray.append(url!)
        }
        return tempArray
    }
    
    /** 被转发的原微博信息字段，当该微博为转发微博时返回 */
    var retweeted_status: Status?
    
    var pictureURLs: [NSURL]? {
        if retweeted_status != nil {
            return retweeted_status?.imageURLs
        }
        return imageURLs
    }
    
    var weiboSource: String? {
        if retweeted_status != nil {
            return retweeted_status?.source
        }
        return source
    }
    
    init(dict: [String: AnyObject]) {
        super.init()
        self.setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            if let dict = value as? [String: AnyObject] {
                user = User(dict: dict)
            }
            return
        }
        if key == "retweeted_status" {
            if let dict = value as? [String: AnyObject] {
                retweeted_status = Status(dict: dict)
            }
            return
        }
        super.setValue(value, forKey: key)
    }
    
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) { }
}
